package com.ftar.urgent.viewmodel;

import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.lifecycle.MutableLiveData;

import com.ftar.urgent.api.KnowledgeApi;
import com.ftar.urgent.api.req.KnowledageCommonReq;
import com.ftar.urgent.api.resp.CheckItemResp;
import com.ftar.urgent.api.resp.ItemRecordsVo;
import com.ftar.urgent.databinding.ActivityAllItemBinding;
import com.ftar.urgent.databinding.ActivityAllitemCommaBinding;
import com.ftar.urgent.databinding.ActivityBaseBinding;
import com.rokid.utils.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.ListUtil;


public class AllItemActivityModel extends BaseModel {

    private String TAG = this.getClass().getSimpleName();

    public MutableLiveData<List<ItemRecordsVo>> mRootList = new MutableLiveData<>(new ArrayList<>());

    private Context mContext;
    private ActivityAllItemBinding mBinding;
    private ActivityBaseBinding mRootBing;
    private ActivityAllitemCommaBinding mCommaBinding;
    private List<List<ItemRecordsVo>> pageList = new ArrayList<>();
    public ItemRecordsVo resultViewData = new ItemRecordsVo();
    public MutableLiveData<List<ItemRecordsVo>> anXinData = new MutableLiveData<>();

    public static final int STEP_INDEX = 1; // 全检测项列表页面
    public static final int STEP_DETAIL = 2; // 子项查询明细页面
    public static final int STEP_DETAIL_MORE = 3; // 子项更多页面
    public static final int STEP_RESULT_EMPTY = 5; // 子项无结果页面
    private int step = 1;

    private int currentPage = 1;
    private int totalSize = 0;
    private int pageSize = 10;
    private int totalPage = 1;
    private String currentPageStr;
    private String totalPageStr;

    private int currentPageResult = 1;
    private int totalSizeResult = 0;
    private int pageSizeResult = 1;
    private int totalPageResult = 1;
    private String currentPageStrResult;
    private String totalPageStrResult;

    public void resetResultPageInfo(){
        this.totalSizeResult = 0;
        this.currentPageResult = 1;
        this.pageSizeResult = 0;
        this.totalPageResult = 1;
    }

    public AllItemActivityModel(Context context, ActivityAllItemBinding binding, ActivityBaseBinding mRootBing, ActivityAllitemCommaBinding commaBinding) {
        this.mContext = context;
        this.mBinding = binding;
        this.mRootBing = mRootBing;
        this.mCommaBinding = commaBinding;
    }

    public Context getmContext() {
        return mContext;
    }

    public ActivityAllItemBinding getmBinding() {
        return mBinding;
    }

    public List<List<ItemRecordsVo>> getPageList() {
        return pageList;
    }

    public List<ItemRecordsVo> getmRootList() {
        return mRootList.getValue();
    }

    public int getTotalSize() {
        return totalSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getCurrentPageResult() {
        return currentPageResult;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public void setTotalSize(int totalSize) {
        this.totalSize = totalSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    /**
     * 初始化全检测项数据
     */
    public void initAllItemData() {
        AllItemActivityModel model = this;
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<ItemRecordsVo> datas = new ArrayList<>();
                boolean fetchFlag = true;
                int page = 1;
                while (fetchFlag) {
                    try {
                        KnowledageCommonReq req = new KnowledageCommonReq();
                        req.setClassifyCode(mBinding.getHandler().parentCode);
                        req.setTypeCode(mBinding.getHandler().currentCode);
                        req.setPage(page);
                        req.setSize(1000);
                        CheckItemResp checkItemResp = KnowledgeApi.getProjectsCategory(req);
                        if (checkItemResp.getRecords() == null) {
                            checkItemResp.setRecords(new ArrayList<>());
                        }
                        totalSize = checkItemResp.getTotal();
                        totalPage = (totalSize + pageSize - 1) / pageSize;
                        if (checkItemResp.getCurrent().equals(checkItemResp.getPages())) {
                            fetchFlag = false;
                        }
                        model.setTotalPageStr();
                        model.setCurrentPageStr();
                        datas.addAll(checkItemResp.getRecords());
                        page++;
                    } catch (IOException e) {
                        fetchFlag = false;
                        Log.e(TAG, String.format("current data page is %d,initAllItemData error!!!", page));
                    }
                }
                pageList = ListUtil.partition(datas, pageSize);
                List<ItemRecordsVo> postList = new ArrayList<>();
                if (pageList.size() > 0) {
                    postList = pageList.get(getCurrentPage() - 1);
                }
                mRootList.postValue(postList);
                mBinding.setAllItemModel(model);
            }
        }).start();
    }

    public void setCurrentPageStr() {
        this.currentPageStr = String.format("第%s页", this.currentPage);
    }

    public void setTotalPageStr() {
        this.totalPageStr = String.format("共%s页", this.totalPage);
    }

    public String getCurrentPageStr() {
        return currentPageStr;
    }

    public String getTotalPageStr() {
        return totalPageStr;
    }

    public void setCurrentPageStrResult() {
        this.currentPageStrResult = String.format("第%s页", this.currentPageResult);
    }

    public void setTotalPageStrResult() {
        this.totalPageStrResult = String.format("共%s页", this.totalPageResult);
    }

    public String getCurrentPageStrResult() {
        return currentPageStrResult;
    }

    public String getTotalPageStrResult() {
        return totalPageStrResult;
    }

    public void goResultPreviousPage() {
        if (this.currentPageResult == 1) {
            return;
        }
        this.currentPageResult--;
        setCurrentPageStrResult();
        resultViewData = (anXinData.getValue().get(currentPageResult - 1));
        this.mBinding.setAllItemModel(this);
    }

    public void goResultNextPage() {
        if (this.currentPageResult == this.totalPageResult) {
            return;
        }
        this.currentPageResult++;
        setCurrentPageStrResult();
        resultViewData = (anXinData.getValue().get(this.currentPageResult - 1));
        this.mBinding.setAllItemModel(this);
    }


    public void goPreviousPage() {
        if (this.currentPage == 1) {
            return;
        }
        this.currentPage--;
        setCurrentPageStr();
        mRootList.postValue(this.pageList.get(currentPage - 1));
        this.mBinding.setAllItemModel(this);
    }

    public void goNextPage() {
        if (this.currentPage == this.totalPage) {
            return;
        }
        this.currentPage++;
        setCurrentPageStr();
        mRootList.postValue(this.pageList.get(this.currentPage - 1));
        this.mBinding.setAllItemModel(this);
    }

    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
        Log.e(TAG, "---------------- step:" + step);
        mRootBing.customCommand.setVisibility(View.GONE);
        mRootBing.voiceHint.setVisibility(View.GONE);
        mRootBing.previousPage.setVisibility(View.GONE);
        mRootBing.nextPage.setVisibility(View.GONE);
        mBinding.pageView.setVisibility(View.GONE);
        mBinding.allItemGridview.setVisibility(View.GONE);
        mBinding.resultEmpty.setVisibility(View.GONE);
        mBinding.resultMoreLy.setVisibility(View.GONE);
        mBinding.resultMoreDetailView.setVisibility(View.GONE);
        mBinding.resultTitle.setVisibility(View.GONE);
        mBinding.resultTableLy.setVisibility(View.GONE);
        mBinding.resultMoreLy.setVisibility(View.GONE);
        mBinding.resultPageLy.setVisibility(View.GONE);
        mBinding.resultLayout.setVisibility(View.GONE);
        mCommaBinding.commandCheckOver.setVisibility(View.GONE);
        mCommaBinding.commandEnd.setVisibility(View.GONE);
        mCommaBinding.commandMore.setVisibility(View.GONE);
        switch (step) {
            case STEP_INDEX:
                mRootBing.voiceHint.setVisibility(View.VISIBLE);
                mBinding.pageView.setVisibility(View.VISIBLE);
                mBinding.allItemGridview.setVisibility(View.VISIBLE);
                mRootBing.previousPage.setVisibility(View.VISIBLE);
                mRootBing.nextPage.setVisibility(View.VISIBLE);
                mRootBing.clickBackBtn.setVisibility(View.VISIBLE);
                break;
            case STEP_DETAIL:
                mRootBing.customCommand.setVisibility(View.VISIBLE);
                mRootBing.previousPage.setVisibility(View.VISIBLE);
                mRootBing.nextPage.setVisibility(View.VISIBLE);
                mBinding.resultLayout.setVisibility(View.VISIBLE);
                mBinding.resultMoreLy.setVisibility(View.VISIBLE);
                mBinding.resultTableLy.setVisibility(View.VISIBLE);
                mBinding.resultTitle.setVisibility(View.VISIBLE);
                mBinding.resultPageLy.setVisibility(View.VISIBLE);
                mCommaBinding.commandCheckOver.setVisibility(View.VISIBLE);
                mCommaBinding.commandMore.setVisibility(View.VISIBLE);
                break;
            case STEP_DETAIL_MORE:
                mBinding.resultMoreDetailView.setVisibility(View.VISIBLE);
                break;
            case STEP_RESULT_EMPTY:
                mCommaBinding.commandEnd.setVisibility(View.VISIBLE);
                mBinding.resultTitle.setVisibility(View.VISIBLE);
                mBinding.resultEmpty.setVisibility(View.VISIBLE);
                break;
        }
    }

    public void sendToAnXin(String word) {
        if(StringUtils.isNullOrEmpty(word)){
            return;
        }
        AllItemActivityModel model = this;
        new Thread(new Runnable() {
            @Override
            public void run() {
                KnowledageCommonReq req = new KnowledageCommonReq();
                req.setClassifyCode(mBinding.getHandler().parentCode);
                req.setTypeCode(mBinding.getHandler().currentCode);
                req.setWord(word);
                req.setSize(1000);
                try {
                    CheckItemResp checkItemResp = KnowledgeApi.queryVoice(req);
                    if (checkItemResp.getRecords() == null) {
                        checkItemResp.setRecords(new ArrayList<>());
                    }
                    totalSizeResult = checkItemResp.getTotal();
                    // 每页一个
                    totalPageResult = totalSizeResult;
                    model.setTotalPageStrResult();
                    model.setCurrentPageStrResult();
                    anXinData.postValue(checkItemResp.getRecords());
                    if (checkItemResp.getRecords().size() > 0) {
                        resultViewData = (checkItemResp.getRecords().get(getCurrentPageResult() - 1));
                    } else {
                        resultViewData = new ItemRecordsVo();
                    }
                    mBinding.setAllItemModel(model);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

}
